(async function(){//异步，视频里面这么写的
//导入包
const Koa = require("koa");//主要
const router = require('./router/main');//接口写这里了
const Bodyparser = require('koa-bodyparser');//解析请求
const StaticCache = require('koa-static-cache');//静态资源
const Session = require('koa-session');//session
const path = require('path');//绝对路径
const formidable = require('koa2-formidable');//解析上传图片

const app = new Koa();//建立接口app

//配置上传图片信息，文档这么写的
app.use (formidable ({
  uploadDir:'./public/img/cook/',//上传图片地址
  keepExtensions: true,
}))

//session，密钥 xjw very good
app.keys = ['xjw very good'];

//配置静态资源位置，方便前端查看图片
app.use(StaticCache(__dirname + '/public', {
  prefix: '/public',
  gzip: true,
}));

//cookie设置，session看不懂
const CONFIG = {
  key: 'koa:sess',   //cookie key (default is koa:sess)
  maxAge: 86400000,  // cookie的过期时间 maxAge in ms (default is 1 days)
  overwrite: true,  //是否可以overwrite    (默认default true)
  httpOnly: true, //cookie是否只有服务器端可以访问 httpOnly or not (default true)
  signed: true,   //签名默认true
  rolling: false,  //在每次请求时强行设置cookie，这将重置cookie过期时间（默认：false）
  renew: false,  //(boolean) renew session when session is nearly expired,
};

//配置session
app.use(Session(CONFIG, app));

//配置解析上传图片
app.use(Bodyparser({multipart: true}));

//配置路由，主要的东西
app.use(router.routes());

//配置端口号，3000
app.listen(3000);
})()